package com.dominator.controller.backend;

import com.dominator.service.UserService;
import com.dominator.utils.exception.ApiException;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.JsonUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by czh on 2017/8/25.
 */
@RestController
@RequestMapping("/backend/v1/user")
public class UserController {

    private final static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 根据手机号检查用户是否存在
     * @param paramStr phone String 必传 手机号
     * @return
     */
    @GetMapping("/checkExist")
    public ApiMessage checkExist(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            ApiUtils.checkParam(dto, "phone");
            msg = userService.checkExist(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 注册
     *
     * @param paramStr phone String 必传 11位手机号
     *                 nickname String 必传 昵称
     *                 head_img_url String 非必传 头像url
     *                 gender String 非必传 0女 1男
     * @return
     */
    @PostMapping("/register")
    public ApiMessage register(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            ApiUtils.checkParam(dto, "phone,nickname");
            msg = userService.register(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 获取用户信息
     *
     * @param paramStr phone String 必传 手机号
     * @return
     */
    @GetMapping("/getUser")
    public ApiMessage getUser(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            ApiUtils.checkParam(dto, "phone");
            msg = userService.getUser(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 修改用户信息
     *
     * @param paramStr user_id String 必传 用户id
     *                 nickname String 必传 昵称
     *                 head_img_url String 非必传 头像url
     *                 gender String 非必传 0女 1男
     * @return
     */
    @PostMapping("/editUser")
    public ApiMessage editUser(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            ApiUtils.checkParam(dto, "user_id,nickname");
            msg = userService.editUser(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
            e.printStackTrace();
        }
        return msg;
    }
}
